from CvPythonExtensions import *

import GerikSpells
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSTargetting as GSHandler
from GSResultsPolicies import GSMission as GSResults

from GSTargetInfoPolicies import GSSinglePlotTarget
from GSTargetInfoPolicies import GSSpecificUnitTarget

from GSGraphicsPolicies import GSPlot
from GSGraphicsPolicies import GSPieMenu

import GerikSpellsUtils

from CivcraftSCUtils import checkAndRemoveEnergy
from CivcraftSCUtils import showNotEnoughEnergyMsg

gc = CyGlobalContext()
localtext = CyTranslator()

class GhostBase(GerikSpellBase, GSGroup, GSHandler):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)


        self.m_iGhostType = gc.getInfoTypeForString("UNIT_GHOST")
            
    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not pCastingUnit.getUnitType() == self.m_iGhostType:
            return False

        # Show this for all ghosts.
        if bVisible:
            return True

        return True

class Lockdown(GhostBase, GSResults):
    def __init__(self):
        GhostBase.__init__(self)

        # Constants
        self.m_iLockdownEnergyCost = 75
        self.m_iLockdownRange = 5
        self.m_iLockdownUnitAttachment = gc.getInfoTypeForString("UNIT_ATTACHMENT_LOCKDOWN")

        self.m_iResearchNeeded = gc.getInfoTypeForString("PROJECT_TERRAN_GHOST_LOCKDOWN")
        self.m_iResearchBuildingToBuildAt = gc.getProjectInfo(self.m_iResearchNeeded).getBuildingToBuildAt()
        self.m_szNotValidText = localtext.getText( "TXT_KEY_CIVCRAFT_REQUIRES_RESEARCH_AT", (gc.getProjectInfo(self.m_iResearchNeeded).getDescription(),
                                                                                             gc.getBuildingInfo(self.m_iResearchBuildingToBuildAt).getDescription()) )

        # Policies
        pSpecificUnitTargetPolicy = GSSpecificUnitTarget(self.canTargetUnit)
        GSResults.__init__(self, pSpecificUnitTargetPolicy)

    def getHelpText(self, pGroup, pPlot):
        bValid = self.canGroupCast(pGroup, pPlot, False)
        if not bValid:
            return self.m_szNotValidText

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not GhostBase.canUnitCast(self, pCastingUnit, pPlot, bVisible):
            return False

        if bVisible:
            return True

        # Check if they have the research
        if not gc.getPlayer( pCastingUnit.getOwner() ).hasBuiltProject( self.m_iResearchNeeded ):
            return False

        return True

    def canTargetPlot(self, pCastingGroup, pCastingPlot, pTargetPlot, bVisible):
        return True

    def canTargetUnit(self, pCastingGroup, pCastingPlot, pTargetUnit, bVisible):
        if bVisible:
            return True

        # Check for invulnerable units
        if pTargetUnit.isInvulnerable():
            return False

        # Only be able to cast on mechanical units.
        if not gc.getUnitInfo(pTargetUnit.getUnitType()).isMechanical():
            return False

        return True

    def getRangePlotList(self, pCastingPlot):
        return GerikSpellsUtils.getRangePlotDistanceList(pCastingPlot, self.m_iLockdownRange)

    def createTargettingStageList(self, bAlt, bShift, bCtrl):
        pStageList = []

        pPlotTargetPolicy = GSSinglePlotTarget(self.canTargetPlot)
        pPlotTargetPolicy.setRangeMakerCallback(self.getRangePlotList)
        pPlotTargetPolicy.setCheckRange(True)

        pPlotGraphicsPolicy = GSPlot()
        pPlotGraphicsPolicy.setShowRangeGraphics(True)
        pPlotGraphicsPolicy.setCursorType("CURSOR_SINGLE_ATTACK")
        
        pStageList.append( (pPlotTargetPolicy, pPlotGraphicsPolicy) )

        pUnitTargetPolicy = GSSpecificUnitTarget(self.canTargetUnit)
        pUnitMenuPolicy = GSPieMenu()
        pStageList.append( (pUnitTargetPolicy, pUnitMenuPolicy) )

        return pStageList

    def startSpellResults(self, pCastingGroup, pTargetInfo):
        bShowNotEnoughEnergy = False
        
        iPlayer = pCastingGroup.getOwner()
        for pUnit in pCastingGroup.getUnits():
            if checkAndRemoveEnergy(pUnit, self.m_iLockdownEnergyCost):
                print pTargetInfo.getTarget()
                for pUnit in pTargetInfo.getAffectedTargetList(pCastingGroup, pCastingGroup.plot()):
                    
                    pUnit.attachUnitAttachment(iPlayer, self.m_iLockdownUnitAttachment)
            else:
                bShowNotEnoughEnergy = True

        if bShowNotEnoughEnergy:
            showNotEnoughEnergyMsg(iPlayer)
            


        

                        
            
        
            
            
